<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #chart {
            width: 500px;
            height: 400px;
            outline: 1px solid red;
        }
    </style>
</head>
<body>
    <div id="chart"></div>

    <script src="../lib/echart.min.js"></script>
    <script>

        // const data = [
        //     {
        //         sex: '男',
        //         v: Math.floor(Math.random() * 200),
        //     },
        //     {
        //         sex: '女',
        //         v: Math.floor(Math.random() * 200),
        //     },
        // ];

        function getData () {
            return [
                {
                    sex: '男',
                    v: Math.floor(Math.random() * 200),
                },
                {
                    sex: '女',
                    v: Math.floor(Math.random() * 200),
                },
            ];
        }

        // 1. 初始化echarts容器
        const chartContainer = document.querySelector('#chart');
        const chart = echarts.init(chartContainer);

        // 2. 写配置项
        function getOptions () {
            return {
                tooltip: {
                    trigger: 'item',
                },
                legend: {
                    top: '5%',
                    left: 'center',
                },
                series: [
                    {
                        name: 'Access From',
                        type: 'pie',
                        radius: ['0%', '70%'],
                        data: getData().map(item => ({ name: item.sex, value: item.v })),
                    },
                ],
            };
        }

        // 3. 导入配置项
        chart.setOption(getOptions());

        setInterval(() => {
            console.log(1);
            chart.setOption(getOptions());
        }, 2000);

    </script>
</body>
</html>